package com.zzyl.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色信息
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页",notes = "角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleVo>> selectPage(@RequestBody RoleDto roleDto,
                                                           @PathVariable Integer pageNum,
                                                           @PathVariable Integer pageSize){
        PageResponse<RoleVo> pageResponse =roleService.selectPage(roleDto,pageNum,pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 新增角色
     * @param roleDto
     * @return
     */
    @PutMapping
    @ApiOperation("新增角色")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState"})
    public ResponseResult insertRole(@RequestBody RoleDto roleDto){
        roleService.add(roleDto);
        return ResponseResult.success();
    }

    /**
     * 根据roleId查询具体资源
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<String>> findCheckedResource(@PathVariable("roleId") Long roleId){
        List<String> list = roleService.getByRoleId(roleId);
        return ResponseResult.success(list);
    }

    /**
     * 根据roleId删除
     * @param roleId
     * @return
     */
    @ApiOperation("删除角色")
    @DeleteMapping("/{roleId}")
    public ResponseResult deleteRole(@PathVariable("roleId") Long roleId){
        roleService.deleteRole(roleId);
        return ResponseResult.success();
    }

    /**
     * 修改角色
     * @param roleDto
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "角色修改",notes = "角色修改")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState","roleDto.dataScope","roleDto.checkedResourceNos","roleDto.checkedDeptNos","roleDto.id"})
    public ResponseResult<Boolean> updateRole(@RequestBody RoleDto roleDto) {
        Boolean flag =  roleService.update(roleDto);
        return ResponseResult.success(flag);
    }

    /**
     * 获取所有的角色
     * @return
     */
    @PostMapping("/init-roles")
    @ApiOperation(value = "获取所有的角色",notes = "获取所有的角色")
    public ResponseResult<List<RoleVo>> getAllRoles(){
        List<RoleVo> list = roleService.getAllRoles();
        return ResponseResult.success(list);
    }
}
